package org.iwidget.desktop.core.javascript;
import org.iwidget.desktop.core.*;
import org.iwidget.desktop.model.WidgetElement;
import java.io.File;
import java.util.StringTokenizer;
import org.mozilla.javascript.*;
/**
 *
 * @author Muhammad Hakim A
 */


public class IwidgetSystem extends ScriptableObject
{

    public String getClassName()
    {
        return "system";
    }

    public IwidgetSystem()
    {
    }

    public void jsConstructor()
    {
    }

    private void setWidget()
    {
        if(widget == null)
        {
            Scriptable scope = ScriptableObject.getTopLevelScope(this);
            Object obj = scope.get("widget", scope);
            widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        }
    }

    public void jsSet_OSName(String s)
    {
    }

    public String jsGet_OSName()
    {
        return getOSName();
    }

    public String jsFunction_getOSName()
    {
        return getOSName();
    }

    public String getOSName()
    {
        return System.getProperty("os.name");
    }

    public void jsSet_OSVersion(String s)
    {
    }

    public String jsGet_OSVersion()
    {
        return getOSVersion();
    }

    public String jsFunction_getOSVersion()
    {
        return getOSVersion();
    }

    public String getOSVersion()
    {
        return System.getProperty("os.version");
    }

    public void jsSet_OSArchitecture(String s)
    {
    }

    public String jsGet_OSArchitecture()
    {
        return getOSArchitecture();
    }

    public String jsFunction_getOSArchitecture()
    {
        return getOSArchitecture();
    }

    public String getOSArchitecture()
    {
        return System.getProperty("os.arch");
    }

    public void jsSet_pathSeparator(String s)
    {
    }

    public String jsGet_pathSeparator()
    {
        return getPathSeparator();
    }

    public String jsFunction_getPathSeparator()
    {
        return getPathSeparator();
    }

    public String getPathSeparator()
    {
        return System.getProperty("path.separator");
    }

    public void jsSet_fileSeparator(String s)
    {
    }

    public String jsGet_fileSeparator()
    {
        return getFileSeparator();
    }

    public String jsFunction_getFileSeparator()
    {
        return getFileSeparator();
    }

    public String getFileSeparator()
    {
        return System.getProperty("file.separator");
    }

    public void jsSet_userName(String s)
    {
    }

    public String jsGet_userName()
    {
        return getUserName();
    }

    public String jsFunction_getUserName()
    {
        return getUserName();
    }

    public String getUserName()
    {
        return System.getProperty("user.name");
    }

    public void jsSet_userHomeFolder(String s)
    {
    }

    public String jsGet_userHomeFolder()
    {
        return getUserHomeFolder();
    }

    public String jsFunction_getUserHomeFolder()
    {
        return getUserHomeFolder();
    }

    public String getUserHomeFolder()
    {
        return System.getProperty("user.home");
    }

    public void jsSet_userCurrentFolder(String s)
    {
    }

    public String jsGet_userCurrentFolder()
    {
        return getUserCurrentFolder();
    }

    public String jsFunction_getUserCurrentFolder()
    {
        return getUserCurrentFolder();
    }

    public String getUserCurrentFolder()
    {
        return System.getProperty("user.dir");
    }

    public void jsSet_rootFolders(String s)
    {
    }

    public String[] jsGet_rootFolders()
    {
        return getRootFolders();
    }

    public String[] jsFunction_getRootFolders()
    {
        return getRootFolders();
    }

    public String[] getRootFolders()
    {
        File roots[] = File.listRoots();
        String rootFiles[] = new String[roots.length];
        for(int iCtr = 0; iCtr < roots.length; iCtr++)
            rootFiles[iCtr] = roots[iCtr].getAbsolutePath();

        return rootFiles;
    }

    public boolean jsFunction_isFile(String pathName)
    {
        return isFile(pathName);
    }

    public boolean isFile(String pathName)
    {
        File file = new File(pathName);
        return file.isFile();
    }

    public boolean jsFunction_canReadFile(String pathName)
    {
        return canReadFile(pathName);
    }

    public boolean canReadFile(String pathName)
    {
        File file = new File(pathName);
        return file.canRead();
    }

    public boolean jsFunction_canWriteFile(String pathName)
    {
        return canWriteFile(pathName);
    }

    public boolean canWriteFile(String pathName)
    {
        File file = new File(pathName);
        return file.canWrite();
    }

    public String jsFunction_stringReplace(String s, String s1, String s2)
    {
        return stringReplace(s, s1, s2);
    }

    public String stringReplace(String s, String s1, String s2)
    {
        String output = Iwidget.replace(s, s1, s2, 0);
        return output;
    }

    public void jsSet_runResult(String s1)
    {
    }

    public String jsGet_runResult()
    {
        return getRunResult();
    }

    public String getRunResult()
    {
        setWidget();
        return widget.getIwidgetData().runResult;
    }

    public void jsSet_key(char c)
    {
    }

    public char jsGet_key()
    {
        setWidget();
        return widget.getIwidgetData().key;
    }

    public void jsSet_keyString(String s1)
    {
    }

    public String jsGet_keyString()
    {
        setWidget();
        return widget.getIwidgetData().keyString;
    }

    public void jsSet_keyModifiers(String s1)
    {
    }

    public String jsGet_keyModifiers()
    {
        setWidget();
        return widget.getIwidgetData().keyModifiers;
    }

    public void jsSet_droppedObjectType(String s1)
    {
    }

    public String jsGet_droppedObjectType()
    {
        return getDroppedObjectType();
    }

    public String jsFunction_getDroppedObjectType()
    {
        return getDroppedObjectType();
    }

    public String getDroppedObjectType()
    {
        setWidget();
        return widget.getIwidgetData().droppedObjectsType;
    }

    public void jsSet_droppedObjects(String as[])
    {
    }

    public String[] jsGet_droppedObjects()
    {
        return getDroppedObjects();
    }

    public String[] jsFunction_getDroppedObjects()
    {
        return getDroppedObjects();
    }

    public String[] getDroppedObjects()
    {
        setWidget();
        return widget.getIwidgetData().droppedObjects;
    }

    public long jsFunction_diskFree(String path)
    {
        try{
            if(System.getProperty("os.name").startsWith("Windows"))
            {
                if(path == null || path.length() != 2 || path.charAt(1) != ':')
                    path = "c:";
                String dir = IwidgetMethods.runInternal("dir /-C " + path);
                dir = dir.substring(0, dir.length() - 1).trim();
                int index = dir.lastIndexOf('\n');
                dir = dir.substring(index, dir.length() - 11);
                index = dir.lastIndexOf(' ');
                dir = dir.substring(index + 1, dir.length());
                if(dir == null || dir.length() == 0)
                    return 0L;
                else
                    return Long.parseLong(dir);
            }
            if(path == null)
                path = "/";
            StringTokenizer tokens;
            String dir = IwidgetMethods.runInternal("df " + path);
            int index = dir.indexOf('\r');
            dir = dir.substring(index + 2, dir.length());
            tokens = new StringTokenizer(" \t");
            tokens.nextElement();
            tokens.nextElement();
            tokens.nextElement();
            return Long.parseLong((String)tokens.nextElement());
        }catch(Exception e){
            return 0L;
        }
    }

    private static final long serialVersionUID = 0x3232323339353935L;
    private WidgetElement widget;
}